package com.hd.trans.framework.dialog

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import com.hd.trans.R
import com.hd.trans.utils.DisplayUtils
import java.util.*

abstract class BaseDialogFragment<T : ViewDataBinding>(@LayoutRes private val layoutId: Int): DialogFragment(), View.OnClickListener {

    protected lateinit var dataBinding: T
    private var mRootView: View? = null
    protected val mHandler: Handler = Handler(Looper.getMainLooper())


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        dataBinding = DataBindingUtil.inflate(LayoutInflater.from(context), layoutId, container, false)
        Objects.requireNonNull(dialog)?.setCanceledOnTouchOutside(canceledOutside)
        mRootView = dataBinding.root
        initWindow()
        return mRootView
    }

    private fun initWindow() {
        val window = Objects.requireNonNull(dialog)?.window
        window?.let { window ->
            window.setBackgroundDrawableResource(R.color.transparent)
            window.decorView.setPadding(paddingStart, 0, paddingEnd, 0)
            val wlp = window.attributes
            wlp.width = windowWidth
            wlp.height = windowHeight
            wlp.gravity = gravity
            wlp.windowAnimations = dialogAnim
            window.attributes = wlp
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    protected fun showToast(string: String) {
        Toast.makeText(activity, string, Toast.LENGTH_SHORT).show()
    }

    open val canceledOutside: Boolean
        get() = true
    open val dialogAnim: Int
        get() = -1
    open val gravity : Int
        get() = Gravity.CENTER
    open val paddingStart: Int
        get() = DisplayUtils.dp2px(requireContext(), 0f)
    open val paddingEnd: Int
        get() = DisplayUtils.dp2px(requireContext(), 0f)
    open fun <T : View?> getView(@IdRes id: Int): T {
        return mRootView!!.findViewById<T>(id)
    }
    open val scaleWidth: Float
        get() = 1.0f
    /**
     * @return 窗口的宽度
     */
    open val windowWidth: Int
        get() {
            val dm = DisplayMetrics()
            activity?.windowManager?.defaultDisplay?.getMetrics(dm)
            return (scaleWidth * dm.widthPixels).toInt()
        }

    /**
     * @return 窗口的高度
     */
    open val windowHeight: Int
        get() = ViewGroup.LayoutParams.WRAP_CONTENT

    abstract fun initView()

    override fun onClick(v: View?) {
    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }
}